package cn.toguide.ffmpeg.component;


import cn.toguide.ffmpeg.config.FFMpegSDKProperties;
import cn.toguide.ffmpeg.constant.CommandType;
import cn.toguide.ffmpeg.constant.MediaFormat;
import cn.toguide.ffmpeg.constant.SampleRateConstants;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;


/**
 * @author wangxiaosong
 * @date 2024-05-13
 */
@Slf4j
@Component
public class FFMpegExecutor {

    @Resource
    private CommandManager commandManager;

    @Resource
    private ResourceManager fileManager;

    public String uniToMp3(String inputPath, int sampleRate) {
        String extension = FilenameUtils.getExtension(inputPath);
        if (extension.equalsIgnoreCase(MediaFormat.PCM)){
            return pcmToMp3(inputPath, sampleRate);
        }
        return transcode(inputPath, MediaFormat.MP3, CommandType.UNI_TO_MP3);
    }

    public String pcmToMp3(String inputPath, int sampleRate) {
        if (sampleRate == SampleRateConstants.SAMPLE_RATE_16) {
            return transcode(inputPath, MediaFormat.MP3, CommandType.PCM16K_TO_MP3);
        } else {
            return transcode(inputPath, MediaFormat.MP3, CommandType.PCM8K_TO_MP3);
        }
    }

    /**
     * 通用转码方法
     *
     * @param inputPath    输入文件绝对路径
     * @param targetFormat 输出文件格式
     * @param commandKey   配置文件中定义 ffmpeg-sdk.commands配置项的名称
     *                     示例 : MP4-TO-MP3: ffmpeg -i inputPath -vn -ac 1 -ar 16000 -codec:a libmp3lame -q:a 9 -b:a 128k outputPath
     *                     配置项的名称为 MP4-TO-MP3
     * @return 输出文件绝对路径
     */
    public String transcode(String inputPath, String targetFormat, String commandKey) {
        String outputPath = fileManager.makeFile(targetFormat);
        String command = commandManager.getCommand(commandKey);
        command = commandManager.activeCommand(command, inputPath, outputPath);
        commandManager.exec(command);
        log.info("target file path : {}", outputPath);
        return outputPath;
    }

    /**
     * 调用FFProbe获取媒体文件的时长
     * @param inputPath 媒体文件
     * @return HH:mm:ss
     */
    public String duration(String inputPath) {
        String command = commandManager.getCommand(CommandType.DURATION);
        command = command.replace(FFMpegSDKProperties.INPUT_FILE_REPLACE_KEY, inputPath);
        String duration = commandManager.execR(command);
        return convertToHHMMSS(Double.parseDouble(duration));
    }

    private String convertToHHMMSS(double totalSeconds) {
        // 使用 Duration 计算时间
        Duration duration = Duration.ofSeconds((long) totalSeconds);
        // 将 Duration 转换为 LocalTime
        LocalTime time = LocalTime.ofSecondOfDay(duration.getSeconds());
        // 格式化为 HH:mm:ss
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        return time.format(formatter);
    }

}